package zkk.com.mengqu.fragment;

import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import zkk.com.mengqu.R;
import zkk.com.mengqu.activity.KitchenActivity;
import zkk.com.mengqu.adapter.FamilyFoodAdapter;
import zkk.com.mengqu.manager.APIManager;
import zkk.com.mengqu.models.Food;
import zkk.com.mengqu.models.Kitchen;
import zkk.com.mengqu.util.AddAnimation;
import zkk.com.mengqu.util.NoScrollListView;
import zkk.com.mengqu.view.NXHooldeView;

/**
 * Created by Administrator on 2017/4/22.
 *  住家菜品
 */
public class FamilyFoodFragment extends Fragment implements AddAnimation {
    private View view;
    private Context context;
    private NoScrollListView listView;
    private FamilyFoodAdapter adapter;
    private List<Food> foods = new ArrayList<>();
    private int kid;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_no_scroll_list , null);
        context = getContext();
        Bundle bundle = getArguments();//从activity传过来的Bundle
        kid = bundle.getInt("kid");
        initView();
        getKitchen(kid);
        return view;
    }

    public void initView(){
        listView = (NoScrollListView) view.findViewById(R.id.listview);
        adapter = new FamilyFoodAdapter(context , foods,this);
        listView.setAdapter(adapter);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    //获取指定id的厨房信息
    public void getKitchen(final int kid){
        APIManager.setManager()
//                .getKitchen("kitchen_detail",kid)
                .getFoodOfKitchen(kid)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<Food>>() {
                    @Override
                    public void onCompleted() {
                        Log.w("FamilyFoodFragment","onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.w("onError","onError");
                        e.printStackTrace();

                    }

                    @Override
                    public void onNext(List<Food> foodList) {
                        if (foodList == null || foodList.size() == 0){
                            Toast.makeText(context, "该厨房暂无菜谱", Toast.LENGTH_LONG).show();
                        }else {
                            foods.clear();
                            foods.addAll(foodList);
                            adapter.notifyDataSetChanged();
                            Log.w("listSize" , "" + foodList.get(0).getName());
                            Log.w("listSize" , "" + foods.size());
                        }
                    }
                });
    }

    @Override
    public void addAction(View view) {
        NXHooldeView nxHooldeView = new NXHooldeView(getActivity());
        int position[] = new int[2];
        view.getLocationInWindow(position);
        nxHooldeView.setStartPosition(new Point(position[0], position[1]));
        ViewGroup rootView = (ViewGroup) getActivity().getWindow().getDecorView();
        rootView.addView(nxHooldeView);
        int endPosition[] = new int[2];
        KitchenActivity.shopCount.getLocationInWindow(endPosition);
        nxHooldeView.setEndPosition(new Point(endPosition[0], endPosition[1]));
        nxHooldeView.startBeizerAnimation();
    }
}
